<template>
	<view class="Cate">
		<mySearch @click="gotoSearch"></mySearch>
		<view class="scroll-view-container">
			<!-- 滚筒条左边 -->
			<scroll-view class="scroll-left-view" scroll-y="true" :style="{height: wh + 'px'}">
				<block v-for="(item, i) in cateList" :key="i">
					<view :class="['left-scroll-view-item', i === active ? 'active' : '']" @click="activeChanged(i)">
						{{item.cat_name}}
					</view>
				</block>
			</scroll-view>
			<!-- 滚筒条右边 -->
			<scroll-view scroll-y="true" class="scroll-right-view" :style="{height: wh + 'px'}" :scroll-top="scrollTop">
				<block class="scroll-right-content" v-for="(item2, i2) in cateLevel2" :key="i2">
					<!-- 右边标题区 -->
					<view class="scroll-right-title">
						/{{item2.cat_name}}/
					</view>
					<!-- 右边内容图片区 -->
					<view class="srcoll-right-item">
						<view class="srcoll-right-ImgAndTextBox" v-for="(item3, i3) in item2.children" :key="i3"
							@click="gotoGoodsList(item3)">
							<image :src="item3.cat_icon" mode="widthFix" class="srcoll-right-img"></image>
							<view class="srcoll-right-text">{{item3.cat_name}}</view>
						</view>
					</view>
				</block>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import mySearch from '@/components/my_search/my_search.vue'
	// 导入自己封装的 mixin 模块
	import badgeMix from '@/mixins/tabbar-badge.js'

	export default {
		components: {
			mySearch
		},
		// 将 badgeMix 混入到当前的页面中进行使用
		mixins: [badgeMix],

		data() {
			return {
				// 窗口高度
				wh: 0,
				active: 0,
				cateList: [],
				cateLevel2: [],
				// 距离页面顶部的距离
				scrollTop: 0
			};
		},

		onLoad() {
			// 获取窗口高度
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight - 45
			// 请求cate列表
			this.getCateList()
		},

		methods: {
			async getCateList() {
				// 发起请求
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/categories')
				// 判断是否获取失败
				if (res.meta.status !== 200) return uni.$showMsg()
				// 转存数据
				this.cateList = res.message
				this.cateLevel2 = res.message[0].children
			},

			activeChanged(i) {
				this.active = i
				this.cateLevel2 = this.cateList[i].children
				// 每次切换都进行 0 与 1 的互换 scroll-top每次值一样的话会不执行的
				this.scrollTop = this.scrollTop === 0 ? 1 : 0
			},

			gotoGoodsList(item3) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?cid=' + item3.cat_id
				})
			},

			// 进入搜索页面
			gotoSearch() {
				// console.log('123456')
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;

		.scroll-left-view {
			width: 120px;

			.left-scroll-view-item {
				line-height: 60px;
				background-color: #f7f7f7;
				text-align: center;
				font-size: 12px;

				// 激活项的样式
				&.active {
					background-color: #ffffff;
					position: relative;

					// 渲染激活项左侧的红色指示边线
					&::before {
						content: ' ';
						display: block;
						width: 3px;
						height: 30px;
						background-color: #c00000;
						position: absolute;
						left: 0;
						top: 50%;
						transform: translateY(-50%);
					}
				}
			}
		}

		.scroll-right-view {
			width: 100%;

			.scroll-right-title {
				height: 120rpx;
				wight: 100%;
				line-height: 120rpx;
				text-align: center;
				font-size: 24rpx;
			}

			.srcoll-right-item {
				display: flex;
				flex-wrap: wrap;
				margin-left: 15rpx;
				box-sizing: border-box;
				background-color: #fff;

				.srcoll-right-ImgAndTextBox {
					width: 33.3%;
					height: 120rpx;
					margin-bottom: 10px;
					display: flex;
					flex-direction: column;
					align-items: center;

					.srcoll-right-img {
						width: 120rpx;
						height: 120rpx;
					}

					.srcoll-right-text {
						font-size: 24rpx;
					}
				}
			}
		}
	}
</style>
